package org.example.dormsystem.service.impl;

import org.example.dormsystem.mapper.StudentBoardMapper;
import org.example.dormsystem.mapper.StudentReplyMapper;
import org.example.dormsystem.pojo.StudentBoard;
import org.example.dormsystem.pojo.StudentReply;
import org.example.dormsystem.service.StudentBoardService;
import org.example.dormsystem.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StudentBoardServiceImpl implements StudentBoardService {

    @Autowired
    StudentBoardMapper studentBoardMapper;

    @Autowired
    StudentReplyMapper studentReplyMapper;

    @Override
    public void submit(String message) {//将留言信息添加到数据库中
        Map<String, Object> map = ThreadLocalUtil.get();
        StudentBoard studentBoard = new StudentBoard();
        studentBoard.setMessage(message);
        studentBoard.setPostedBy((Integer) map.get("id"));
        studentBoardMapper.add(studentBoard);
    }

    @Override
    public List<Map<String,Object>> getAll() {//返还所有留言与回复
        List<StudentBoard> list = studentBoardMapper.getAll();
        List<Map<String,Object>> ans = new ArrayList<>();
        for(StudentBoard i: list){
            Map<String,Object> map = new HashMap<>();
            Integer studentBoardId = i.getId();
            List<StudentReply> replys = studentReplyMapper.findReplysByStudentBoardId(studentBoardId);
            map.put("StudentBoard",i);
            map.put("Replys",replys);
            ans.add(map);
        }
        return ans;
    }

    @Override
    public void reply(String message,Integer studentBoardId) {
        StudentReply  studentReply = new StudentReply();
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer studentId = (Integer) map.get("id");
        studentReply.setMessage(message);
        studentReply.setBoardId(studentBoardId);
        studentReply.setPostedBy(studentId);
        studentReplyMapper.add(studentReply);

    }
}
